<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
       
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar @queryTable="getList"></right-toolbar>
    </el-row>

    <vxe-table
      ref="table"
      :data="tableData.list"
      :loading="tableData.loading"
      stripe
    >
      <vxe-column
        field="warehouseNo"
        title="编码"
        width="150"
        align="center"
      />
      <vxe-column
        field="warehouseName"
        title="名称"
        width="260"
        align="center"
      />
      <vxe-column
        field="address"
        title="地址"
        align="center"
      />
      <vxe-column title="操作" width="150">
        <template #default="{ row }">
          <el-button
            size="small"
            type="primary"
            link
            @click="handleUpdate(row)"
          >修改</el-button>
          <el-button
            size="small"
            type="primary"
            link
            @click="handleDelete(row)"
          >删除</el-button>
        </template>
      </vxe-column>
    </vxe-table>

 <div class="pagination-container ">
  <pagination
      v-show="tableData.paging.total > 0"
      :total="tableData.paging.total"
      v-model:page="tableData.paging.pageNum"
      v-model:limit="tableData.paging.pageSize"
      @pagination="getList"
    />
 </div>

    <edit ref="editDialog" @sub="onEditSub"></edit>
  </div>
</template>
<script setup>

import {ref, reactive} from "vue";

import { onMounted } from "vue";
import api from "./api.js";
import Edit from './edit'

const { proxy } = getCurrentInstance();

const editDialog = ref();

// 遮罩层
const loading = ref(false);
const table = ref();
const tableData = reactive({
  loading: false,
  list: [], // 表格列表数据
  paging: {
    total: 0, // 总条数
    pageNum: 1,
    pageSize: 10,
  }
});

const initQueryParams = {
  pageNum: 1,
  pageSize: 10,
  number: null,
  name: null,
  categoryId: null,
  rfid: null,
  brand: null,
  model: null,
  buyWay: null,
  buyTime: null,
  buyPrice: null,
  adminId: null
}

const queryParams = reactive({...initQueryParams})

/** 查询资产清单表列表 */
async function getList() {

  loading.value = true;
  const {pageNum, pageSize} = tableData.paging;
  const query = {...queryParams};
  const pageReq = {page: pageNum - 1, size: pageSize};
  api.list(query, pageReq).then(response => {
    const { content, totalElements } = response
    tableData.list = content;
    tableData.paging.total = totalElements;
    tableData.loading = false;
  }).catch(error => {
    tableData.loading = false;
    console.log(error);
  });
}

/** 新增按钮操作 */
function handleAdd() {
  editDialog.value.open();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  editDialog.value.open(row);
}

/** 编辑窗口提交后的操作 */
function onEditSub() {
  getList();
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || this.ids;
  proxy.$modal.confirm('是否确认删除资产清单表编号为"' + ids + '"的数据项？').then(function() {
    return api.del(ids);
  }).then(() => {
    this.getList();
    this.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  const queryParams = this.queryParams;
  this.$modal.confirm('是否确认导出所有资产清单表数据项？').then(() => {
    this.exportLoading = true;
    return exportAssetItem(queryParams);
  }).then(response => {
    this.$download.download(response);
    this.exportLoading = false;
  }).catch(() => {});
}

// 页面打开后获取列表数据
onMounted( async () => {

  await getList();

});

</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh; 
  overflow: hidden; 
}

.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end; 

}
</style>

